package com.barber.controller;

import com.barber.service.AdminService;
import com.barber.util.SendEmail;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController  // @Controller+@ResponseBody @ResponseBody注解来返回一个JSON对象
@RequestMapping("/admin")
public class AdminController {
    Logger logger = Logger.getLogger(AdminController.class);
    @Autowired
    AdminService adminService;
    @GetMapping("/getinfo")
    Map getinfo(HttpServletRequest request, HttpServletResponse response, @RequestParam Map map) throws IOException {
        logger.debug(map);
        Map	islogin= (Map) request.getSession().getAttribute("islogin");
        logger.debug(islogin);
        map.putAll(islogin);
        logger.debug(map);
        Map m=adminService.getinfo(map);
        logger.debug(m);
        if (m!=null) {
            return m;
        } else {
            return null;
        }
    }
    @GetMapping("/login")//登录hash表返回两个数据
    String alogin(HttpServletRequest request, @RequestParam Map map) throws IOException {
        logger.debug(map);
        Map m=adminService.alogin(map);

        logger.debug(m);

        if (m!=null) {
            request.getSession().setAttribute("islogin",m);
            m.put("usertype",1);
            return  "success";
        } else {
//		request.getSession().setAttribute("selllogin", "mtc_null");
            request.getSession().setAttribute("islogin", null);
            return ("用户名或密码错误");
        }
    }
    @GetMapping("/logout")//登录hash表返回两个数据
    protected void  logout(HttpServletRequest request,HttpServletResponse response, @RequestParam Map map) throws IOException {
        request.getSession().setAttribute("islogin",null);
        response.sendRedirect("bye");

    }

    @PostMapping("/update")
    String update(@RequestParam Map map, HttpServletRequest request)throws IOException {
        logger.debug(map);
        Map m= (Map) request.getSession().getAttribute("islogin");
        logger.debug(m);
//        map.put("lxsellid",m.get("lxid"));
        logger.debug(map);
        Boolean b= adminService.update(map);
        if (b) {
            return ("success");
        } else {
            return ("update failure!");

        }
    }
    @GetMapping("/islogin")
    String islogin(HttpServletRequest req, HttpServletResponse resp, @RequestParam Map map) throws IOException {
        logger.debug(map);
        Map islogin=(Map) req.getSession().getAttribute("islogin");

        logger.debug(map);

        if(islogin==null) {
//			islogin.equals("mtc_null");
            //islogin = "mtc_null";
            return "mtc_null";
        }
        logger.debug(islogin);//{lxid=4, lxtel=null, lxpass=Yi123456, lxaddr=1, lxname=哒玲玲}
        return (islogin.toString());//{{=}}

    }
    @GetMapping("/forgetpwd")//登录hash表返回两个数据
    String forgetpwd(HttpServletRequest request, @RequestParam Map map) throws IOException {
//        logger.debug(map);
                String email= (String) map.get("fpemail");
        logger.debug(email);
        Map map1=adminService.password(map);
        logger.debug(map1);
        if (map1!=null) {
//            request.getSession().setAttribute("email",map1);

            map1.put("usertype",1);
            String password = (String) map1.get("password");
            SendEmail.sendEmail( email,  password);
            return  "success";
        } else {
            return ("FAIL");
        }
    }
}
